﻿#region Copyright and License Notice
// Copyright (C)2010-2016 - Rob Levine and other contributors
// http://configgen.codeplex.com
// 
// This file is part of ConfigGen.
// 
// ConfigGen is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// ConfigGen is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License and 
// the GNU Lesser General Public License along with ConfigGen.  
// If not, see <http://www.gnu.org/licenses/>
#endregion

using System;
using ConfigGen.Core.TokenHandling;
using ConfigGen.Utilities.Extensions.System;
using NUnit.Framework;

namespace ConfigGen.Core.Tests.TokenHandling
{
    [TestFixture]
    public class DelimiterLocatorTests
    {
        private string _locatedDelimiter;

        [Test]
        public void Ctor001_ArgumentNullThrows()
        {
            Assert.Throws<ArgumentNullException>(() => new DelimiterLocator(null));
        }

        [Test]
        public void Ctor002_ArgumentZeroLengthThrows()
        {
            Assert.Throws<ArgumentException>(() => new DelimiterLocator(string.Empty));
        }

        [Test]
        public void Ctor003_DelimiterPropertySetCorrectly()
        {
            var delimiterLocator = new DelimiterLocator("[%");
            Assert.AreEqual("[%", delimiterLocator.Delimiter);
        }

        [Test]
        public void Push001_NonDelimiterIsEchoedAsReturnValue()
        {
            _locatedDelimiter = null;
            var delimiterLocator = new DelimiterLocator("[%");
            delimiterLocator.DelimiterLocated += DelimiterLocatorDelimiterLocated;
            var ret = delimiterLocator.Push('t');
            Assert.IsTrue(new [] {'t'}.IsEqualTo(ret));
            Assert.IsNull(_locatedDelimiter, "No located delimiter expected");
        }

        [Test]
        public void Push002_NotActuallyADelimiter()
        {
            _locatedDelimiter = null;
            var delimiterLocator = new DelimiterLocator("[%");
            delimiterLocator.DelimiterLocated += DelimiterLocatorDelimiterLocated; 

            Assert.IsNull(delimiterLocator.Push('['), "null return expected on first push");
            var ret = delimiterLocator.Push('a');
            Assert.IsTrue(new[] { '[', 'a' }.IsEqualTo(ret), "Incorrect return value on second push");
            Assert.IsNull(_locatedDelimiter, "No located delimiter expected");
        }

        [Test]
        public void Push003_TokenLocated()
        {
            _locatedDelimiter = null;
            var delimiterLocator = new DelimiterLocator("[%");
            delimiterLocator.DelimiterLocated += DelimiterLocatorDelimiterLocated;

            Assert.IsNull(delimiterLocator.Push('['), "null return expected on first push");
            Assert.IsNull(_locatedDelimiter, "No located delimiter expected after first push");
            Assert.IsNull(delimiterLocator.Push('%'), "null return expected on second push");
            Assert.AreEqual("[%", _locatedDelimiter, "Incorrect located delimiter");
        }

        void DelimiterLocatorDelimiterLocated(object sender, DelimiterLocatedEventArgs e)
        {
            _locatedDelimiter = e.Delimiter;
        }
    }
}
